package file.conversion;

import gui.ava.html.Html2Image;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRendererImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import javax.imageio.ImageWriteParam;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @program JavaBase
 * @description: 文本转图片
 * @author: zhanglu
 * @create: 2020-12-22 18:56:00
 */
@Slf4j
public class TextToImage2 {

    @Test
    public void test01() throws Exception {
        HtmlParserImpl parser = new HtmlParserImpl();
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/temp/1.html")));
        String line = "";
        while ((line = br.readLine()) != null){
            sb.append(line);
        }
        parser.loadHtml(sb.toString());
        ImageRendererImpl renderer = new ImageRendererImpl(parser);

        renderer.saveImage("/Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/temp/hello-world1.png");
    }

    @Test
    public void test02() throws Exception {
        HtmlParserImpl parser = new HtmlParserImpl();
        parser.load(new URL("http://blog.showlu.top/router/toArticleDetail/186/0"));
        ImageRendererImpl renderer = new ImageRendererImpl(parser);
        renderer.saveImage("/Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/temp/hello-world.png");
    }

    @Test
    public void test03() throws Exception {
        HtmlParserImpl parser = new HtmlParserImpl();
        String htmlStr = "<html><head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>登录 - sKnowledge管理系统</title>\n" +
                "    <link rel=\"stylesheet\" href=\"/assets/common/layui/css/layui.css\">\n" +
                "    <link rel=\"stylesheet\" href=\"/assets/common/css/login.css\" media=\"all\">\n" +
                "\n" +
                "    <style>\n" +
                "        body {\n" +
                "            background-image: url(\"/assets/common/images/bg_login2.svg\");\n" +
                "            background-position: center 110px;\n" +
                "            background-repeat: no-repeat;\n" +
                "            background-size: 100%;\n" +
                "            background-color: #f0f2f5;\n" +
                "        }\n" +
                "\n" +
                "        .login-header {\n" +
                "            color: rgba(0, 0, 0, .85) !important;\n" +
                "        }\n" +
                "\n" +
                "        .login-footer {\n" +
                "            color: rgba(0, 0, 0, 0.7) !important;\n" +
                "        }\n" +
                "\n" +
                "        .login-footer a {\n" +
                "            color: rgba(0, 0, 0, 0.7) !important;\n" +
                "        }\n" +
                "\n" +
                "        .login-footer a:hover {\n" +
                "            color: rgba(0, 0, 0, 0.4) !important;\n" +
                "        }\n" +
                "    </style>\n" +
                "    <script>\n" +
                "        if (window != top)\n" +
                "            top.location.replace(location.href);\n" +
                "    </script>\n" +
                "    <link id=\"layuicss-layer\" rel=\"stylesheet\" href=\"http://blog.showlu.top/assets/common/layui/css/modules/layer/default/layer.css?v=3.1.1\" media=\"all\"></head>\n" +
                "\n" +
                "<body>\n" +
                "<div class=\"login-wrapper\">\n" +
                "\n" +
                "    <div class=\"login-header\">\n" +
                "        <img src=\"/assets/expand/images/logo.png\"> sKnowledge管理系统\n" +
                "    </div>\n" +
                "\n" +
                "    <div class=\" login-body\">\n" +
                "        <div class=\"layui-card\">\n" +
                "            <div class=\"layui-card-header\">\n" +
                "                <i class=\"layui-icon layui-icon-engine\"></i>&nbsp;&nbsp;用户登录\n" +
                "            </div>\n" +
                "            <form class=\"layui-card-body layui-form layui-form-pane\" action=\"/login\" method=\"post\">\n" +
                "                <div class=\"layui-form-item\">\n" +
                "                    <label class=\"layui-form-label\">账号</label>\n" +
                "                    <div class=\"layui-input-block\">\n" +
                "                        <input name=\"username\" type=\"text\" lay-verify=\"required\" class=\"layui-input\">\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "                <div class=\"layui-form-item\">\n" +
                "                    <label class=\"layui-form-label\">密码</label>\n" +
                "                    <div class=\"layui-input-block\">\n" +
                "                        <input name=\"password\" type=\"password\" lay-verify=\"required\" class=\"layui-input\">\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "                <div class=\"layui-form-item\" pane=\"\">\n" +
                "                    <label class=\"layui-form-label\" lay-tips=\"7天内免登陆\">记住我</label>\n" +
                "                    <div class=\"layui-input-block\">\n" +
                "                        <input type=\"radio\" name=\"remember\" value=\"on\" title=\"是\"><div class=\"layui-unselect layui-form-radio\"><i class=\"layui-anim layui-icon\">\uE63F</i><div>是</div></div>\n" +
                "                        <input type=\"radio\" name=\"remember\" value=\"off\" title=\"否\" checked=\"\"><div class=\"layui-unselect layui-form-radio layui-form-radioed\"><i class=\"layui-anim layui-icon\">\uE643</i><div>否</div></div>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "                <!--<div class=\"layui-form-item\">-->\n" +
                "                <!--<label class=\"layui-form-label\"><i class=\"layui-icon layui-icon-vercode\"></i></label>-->\n" +
                "                <!--<div class=\"layui-input-block\">-->\n" +
                "                <!--<div class=\"layui-row inline-block\">-->\n" +
                "                <!--<div class=\"layui-col-xs7\">-->\n" +
                "                <!--<input name=\"code\" type=\"text\" lay-verify=\"required\" placeholder=\"验证码\"-->\n" +
                "                <!--class=\"layui-input\">-->\n" +
                "                <!--</div>-->\n" +
                "                <!--<div class=\"layui-col-xs5\" style=\"padding-left: 10px;\">-->\n" +
                "                <!--<img class=\"login-captcha\" src=\"https://www.oschina.net/action/user/captcha\">-->\n" +
                "                <!--</div>-->\n" +
                "                <!--</div>-->\n" +
                "                <!--</div>-->\n" +
                "                <!--</div>-->\n" +
                "                <!--<div class=\"layui-form-item\">\n" +
                "                    <a href=\"javascript:;\" class=\"layui-link\">帐号注册</a>\n" +
                "                    <a href=\"/router/home\" class=\"layui-link pull-right\">跳转首页？</a>\n" +
                "                </div>-->\n" +
                "                <div class=\"layui-form-item\">\n" +
                "                    <button lay-filter=\"login-submit\" class=\"layui-btn layui-btn-fluid\" lay-submit=\"\">登 录</button>\n" +
                "                </div>\n" +
                "                <div class=\"layui-form-item login-other\">\n" +
                "                    <label>第三方登录</label>\n" +
                "                    <a href=\"javascript:{layer.msg('暂无功能')};\"><i class=\"layui-icon layui-icon-login-qq\"></i></a>\n" +
                "                    <a href=\"javascript:{layer.msg('暂无功能')};\"><i class=\"layui-icon layui-icon-login-wechat\"></i></a>\n" +
                "                    <a href=\"javascript:{layer.msg('暂无功能')};\"><i class=\"layui-icon layui-icon-login-weibo\"></i></a>\n" +
                "                    <a href=\"/router/home\" class=\"layui-link pull-right\">跳转首页？</a>\n" +
                "                </div>\n" +
                "            </form>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "\n" +
                "    <div class=\"login-footer\">\n" +
                "        <p>© 2018 <a href=\"https://gitee.com/ShowLuu\" target=\"_blank\">ShowLu版权所有</a></p>\n" +
                "    </div>\n" +
                "</div>\n" +
                "\n" +
                "<script type=\"text/javascript\">\n" +
                "    var Feng = {\n" +
                "        ctxPath: \"\",\n" +
                "        addCtx: function (ctx) {\n" +
                "            if (this.ctxPath === \"\") {\n" +
                "                this.ctxPath = ctx;\n" +
                "            }\n" +
                "        }\n" +
                "    };\n" +
                "    Feng.addCtx(\"\");\n" +
                "</script>\n" +
                "<script type=\"text/javascript\" src=\"/assets/common/layui/layui.js\"></script>\n" +
                "<script type=\"text/javascript\" src=\"/assets/common/js/common.js\"></script>\n" +
                "\n" +
                "<script>\n" +
                "    layui.use(['layer', 'form'], function () {\n" +
                "        var $ = layui.jquery;\n" +
                "        var layer = layui.layer;\n" +
                "        var form = layui.form;\n" +
                "\n" +
                "        // 表单提交\n" +
                "        form.on('submit(login-submit)', function (obj) {\n" +
                "            return true;\n" +
                "        });\n" +
                "\n" +
                "        // 图形验证码\n" +
                "        // $('.login-captcha').click(function () {\n" +
                "        //     this.src = this.src + '?t=' + (new Date).getTime();\n" +
                "        // });\n" +
                "\n" +
                "        var errorMsg = \"\";\n" +
                "        if (errorMsg) {\n" +
                "            layer.msg(errorMsg, {icon: 5, anim: 6});\n" +
                "        }\n" +
                "\n" +
                "    });\n" +
                "</script>\n" +
                "\n" +
                "\n" +
                "\n" +
                "<div id=\"color-picker-wrap\" style=\"display: none; position: fixed; top: 0px; left: 0px; z-index: 9999;\"><!----></div></body></html>";
        parser.loadHtml(htmlStr);
        ImageRendererImpl renderer = new ImageRendererImpl(parser);
        renderer.saveImage("/Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/temp/hello-world.png");
    }

    @Test
    public void test04() throws Exception {
        Html2Image html2Image = Html2Image.fromURL(new URL("http://www.qq.com/"));
        html2Image.getImageRenderer().saveImage("/Users/zhanglu/Desktop/lu/workspace/dev_workspace/idea/学习/JavaBase/JavaBase/temp/hello-world2.png");
    }

}
